import { wxRequest } from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    id: null,
    showModal:false
  },


  onLoad: function (options) {
    console.log(options, 'options')
    if (options.id) {
      this.setData({
        id: options.id
      })
    }
  },
  onShow: function () {
    this.getdetail()
  },
  
  //获取详情
  getdetail: function () {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/detail?id=' + this.data.id,
      method: 'POST',
    }, (res) => {
      var resVo = res.vo
      if (typeof resVo.detailJson == 'string') {
        resVo.detailJson = JSON.parse(resVo.detailJson)
      } 
      this.setData({
        detail: resVo
      })
    })
  },


  //退款
  refund:function(e){
    var showModal = this.data.showModal;
    var item = e.currentTarget.dataset.item
    this.setData({
      showModal: !showModal,
      refundReason: '',
      odrItemId: item.id
    })
  },
  


  //监听理由
  onReason: function (e) {
    var inputval = e.detail.value;
    this.setData({
      refundReason: inputval
    })
  },

  //取消
  cancel: function () {
    var showModal = this.data.showModal;
    this.setData({
      showModal: !showModal,
      refundReason: '',
      odrItemId: ''
    })
  },


  //确定
  confirm: function () {
    if (!this.data.refundReason) {
      wx.showToast({
        icon: 'none',
        title: '退款原因不能为空',
      })
      return
    }
    var that = this;
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: "确定执行此操作",
      success: function (res) {
        if (res.confirm) {
          that.setData({
            showModal: false,
          })
          //参数
          if (that.data.odrItemId) {
            // var params = {
            //   odrId: that.data.id,
            //   odrItemId: that.data.odrItemId,
            //   refundReason: that.data.refundReason
            // }
            _wxRequest.wxTonkenAjax({
              url: app.globalData.apiUrl + '/odr/protected/order/refund?odrId=' + that.data.id + '&odrItemId=' + that.data.odrItemId + '&refundReason=' + that.data.refundReason,
              method: 'POST',
              // data: params
            }, (res) => {
              that.getdetail()
              wx.showToast({
                title:'已提交申请，请耐心等待'
              })
            })
          }
        }
      }
    })
  },



  //申请退款
  refundRequest: function (params) { 
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/refund',
      method: 'POST',
      data: params
    }, (res) => {
        console.log(res,'res')
    })
  },


})